package com.eight.sertvice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.feign.UserFeign;
import com.eight.mapper.CouponMapper;
import com.eight.pojo.TbCoupon;
import com.eight.pojo.TbUser;
import com.eight.pojo.TbUserCoupon;
import com.eight.sertvice.CouponService;
import com.eight.util.TimeUtil;
import com.eight.vo.ComResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO
 * @Author: Van.Planifolia
 * @CreateTime: 2023-02-28  16:04
 * @Version: 1.0
 */
@Service("couponService")
public class CouponServiceImpl extends ServiceImpl<CouponMapper, TbCoupon> implements CouponService {
    @Resource(name = "couponMapper")
    private CouponMapper couponMapper;
    @Autowired
    private UserFeign userFeign;

    /**
     * 分页查询数据
     *
     * @param pageIndex 页面索引
     * @param pageSize  页面大小
     * @return 结果
     */
    @Override
    public ComResult getPage(Integer pageIndex, Integer pageSize) {
        Page<TbCoupon> page = new Page<>(pageIndex, pageSize);
        page = couponMapper.selectPage(page, null);
        return new ComResult(2000, "查询成功", page);
    }

    /**
     * 获取用户可用的优惠券
     *
     * @return 优惠券信息
     */
    @Override
    public List<TbCoupon> getUseEnableCoupon() {
        QueryWrapper<TbCoupon> query = new QueryWrapper<>();
        // 查询剩余数量大于0且没过期且指定用户的券
        query.eq("use_type", 2)
                .ge("end_time", TimeUtil.now(""))
                .gt("count", 0);
        return couponMapper.selectList(query);
    }

    /**
     * 进行模糊查询
     *
     * @param tbCoupon  查询条件
     * @param pageIndex 分页索引
     * @param pageSize  分页大小
     * @return 查询的结果
     */
    @Override
    public ComResult fuzzySearchPage(TbCoupon tbCoupon, Integer pageIndex, Integer pageSize) {
        Page<TbCoupon> page = new Page<>(pageIndex, pageSize);
        System.out.println(tbCoupon);
        page = couponMapper.fuzzyPage(page, tbCoupon);
        return new ComResult(2000, "查询成功", page);
    }

    /**
     * 批量查询ids数组中的优惠券
     *
     * @param ids 优惠券id
     * @return 查询的List
     */
    @Override
    public List<TbCoupon> batchSearchById(List<Integer> ids) {
        return couponMapper.selectBatchIds(ids);
    }

    /**
     * 添加一条用户优惠券记录
     *
     * @param tbCoupon 优惠券
     * @param tbUser  用户对象
     * @return 插入结果
     */
    @Override
    @Transactional
    public ComResult addUserCoupon(TbCoupon tbCoupon, TbUser tbUser) {
        // 先插入用户表
        TbUserCoupon tbUserCoupon = new TbUserCoupon().setUserName(tbUser.getUsername())
                .setCouponId(tbCoupon.getId())
                .setUserId(tbUser.getId())
                .setStartTime(tbCoupon.getStartTime())
                .setStopTime(tbCoupon.getEndTime())
                .setCouponName(tbCoupon.getName());
        int i = userFeign.inserUserCon(
                tbUserCoupon

        );
        tbCoupon.setCount(tbCoupon.getCount() - 1);
        // 然后更新优惠券表
        int i1 = couponMapper.updateById(tbCoupon);
        if (i > 0 && i1 > 0) {
            return new ComResult(2000, "领取成功");
        }else {
            // 抛出异常方便回滚事务
            throw new RuntimeException("领取失败！");
        }
    }
}
